package com.cat.model.web.interceptor;

import java.lang.reflect.Method;

import javax.servlet.http.HttpServletResponse;

import com.alonew.core.web.util.ActionMethodUtil;
import com.cat.model.web.action.base.BaseAction;
import com.cat.model.web.annotation.NeedLogin;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class SpringdemoDefaultInterceptor extends AbstractInterceptor {

	private static final long serialVersionUID = -8492579989747450326L;
//	@Autowired
//	private UserService userService;
	
	public String intercept(ActionInvocation ai) throws Exception {
		Method method = ActionMethodUtil.getActionMethod(ai);
		BaseAction action = (BaseAction) ai.getAction();
		HttpServletResponse response = action.getServletResponse();
//		HttpServletRequest request = action.getServletRequest();
		NeedLogin needLogin = null;
		if (method != null) {
			needLogin = method.getAnnotation(NeedLogin.class);
		}
		if (needLogin == null || needLogin.login()) {
			// 需要登录验证
//			TwoTuple<User, LenovoUser> tuple = getUser(request, response);
//			if (tuple != null) {
//				action.initUser(tuple.first);
//				action.initLenovoUser(tuple.second);
//			} else {
//				// 如果host为空,着说明未登陆,或token过期了
//				request.setAttribute("errmsg", "未登陆,验证Key过期");
//				request.setAttribute("cmd", request.getRequestURI());
//				return "tokenExpireException";
//			}
		}
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "");
		response.setDateHeader("Expires", 0);
		return ai.invoke();
	}
	
//	private TwoTuple<User, LenovoUser> getUser(HttpServletRequest request, HttpServletResponse response) {
//		String token = getToken(request);
//		TwoTuple<User, LenovoUser> tuple = userService.getUserByToken(token);
//		return tuple;
//	}

//	private String getToken(HttpServletRequest request) {
//		String token = request.getHeader("token");
//		if(token == null || token.isEmpty()) {
//			token = request.getParameter("token");
//		}
//		return token;
//	}
}
